package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.form.ScBaseCvTankvolumeForm;
import cn.huiyunche.base.service.interfaces.ScBaseCvTankvolumeService;
import cn.huiyunche.base.service.interfaces.query.ScBaseCvQueryConditions;
import cn.huiyunche.base.service.mappers.ScBaseCvTankvolumeMapper;
import cn.huiyunche.base.service.model.ScBaseCvTankvolume;
import cn.huiyunche.base.service.model.ScBaseCvTankvolumeExample;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.ScBaseCvTankvolumeVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/28 0028.
 */
@Service
public class ScBaseCvTankvolumeServiceImpl implements ScBaseCvTankvolumeService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScBaseCvTankvolumeServiceImpl.class);

    @Autowired
    private ScBaseCvTankvolumeMapper scBaseCvTankvolumeMapper = null;


    @Override
    public Map<String, Object> selectListByConditions(PageVo pageVo, ScBaseCvQueryConditions conditions) throws Exception{
        LOGGER.info("ScBaseCvTankvolumeServiceImpl.selectListByConditions params : {}, {}", pageVo, conditions);

        if (null == conditions) {
            LOGGER.error("ScBaseCvTankvolumeServiceImpl.selectListByConditions param conditions must not be null");
            throw new IllegalArgumentException("油箱容积查询条件不能为空!");
        }

        Map<String, Object> map = new HashMap<>();

        String orderByClause = null;
        if (null != pageVo) {
            orderByClause = StringUtils.isNotBlank(pageVo.getOrder()) == true ? pageVo.getOrder() : " id DESC";
        }

        ScBaseCvTankvolumeExample example = new ScBaseCvTankvolumeExample();
        ScBaseCvTankvolumeExample.Criteria criteria = example.createCriteria();
        if (null != conditions.getCvTypeId()) {
            criteria.andCvTypeIdEqualTo(conditions.getCvTypeId());
        }
       if (null != conditions.getTankVolume()) {
            criteria.andTankVolumeEqualTo(conditions.getTankVolume());
        }

        if (null != pageVo) {
            pageVo.setTotalRecord(scBaseCvTankvolumeMapper.countByExample(example));
            example.setLimitStart(pageVo.getStartIndex());
            example.setLimitEnd(pageVo.getPageSize());
            example.setOrderByClause(orderByClause);
        }

        List<ScBaseCvTankvolumeVo> list = scBaseCvTankvolumeMapper.selectByExample(example);
        map.put("list", list);

        if (null != pageVo) {
            map.put("page", pageVo);
        }

        return map;
    }




    @Override
    public Integer add(ScBaseCvTankvolumeForm form, BindingResult br) throws Exception{
        LOGGER.info("ScBaseCvTankvolumeServiceImpl.add param : {}", form);

        if (form == null) {
            LOGGER.error("ScBaseCvTankvolumeServiceImpl.add param form must not be null");
            throw new IllegalArgumentException("表单不能为空");
        }

        //表单验证
        verificationForm(br);

        //验证车型是否唯一
        this.verificationCarTypeUnique(form);


        ScBaseCvTankvolume scBaseCvTankvolume = new ScBaseCvTankvolume();
        BeanUtils.copyProperties(form, scBaseCvTankvolume);
        scBaseCvTankvolumeMapper.insertSelective(scBaseCvTankvolume);

        return scBaseCvTankvolume.getId();

    }

    @Override
    public void update(ScBaseCvTankvolumeForm form, BindingResult br) throws Exception {
        LOGGER.info("ScBaseCvTankvolumeServiceImpl.add param : {}", form);

        if (null == form) {
            LOGGER.error("ScBaseCvTankvolumeServiceImpl.add param form must not be null");
            throw new IllegalArgumentException("车型分类表单不能为空");
        }

        //验证表单
        verificationForm(br);

        //业务验证 车型分类编码唯一
        this.verificationCarTypeUnique(form);

        ScBaseCvTankvolume scBaseCvTankvolume = new ScBaseCvTankvolume();
        BeanUtils.copyProperties(form, scBaseCvTankvolume);
        scBaseCvTankvolumeMapper.updateByPrimaryKeySelective(scBaseCvTankvolume);

    }

    public void verificationCarTypeUnique(ScBaseCvTankvolumeForm form) throws Exception{
        LOGGER.info("ScBaseCvTankvolumeServiceImpl.verificationCarTypeUnique params : {}, {}", form);

        if (form.getCvTypeId() == null) {
            LOGGER.error("ScBaseCvTankvolumeServiceImpl.verificationCarTypeUnique param form must not be null");
            throw new IllegalArgumentException("车型不能为空");
        }

        ScBaseCvTankvolumeExample example = new ScBaseCvTankvolumeExample();
        example.createCriteria().andCvTypeIdEqualTo(form.getCvTypeId());
        List<ScBaseCvTankvolumeVo> list = scBaseCvTankvolumeMapper.selectByExample(example);

        if (CollectionUtils.isNotEmpty(list)) {

            ScBaseCvTankvolumeVo scBaseCvTankvolumeVo = list.get(0);

            //修改时判断 如果id一致则修改时未改变车型
            if (form.getId() != null && form.getId() == scBaseCvTankvolumeVo.getId()) {
                return;
            }
            LOGGER.error("ScBaseCvTankvolumeServiceImpl.verificationCarTypeUnique error list is not empty");
            throw new BusinessException("车型已存在！");
        }
    }


    @Override
    public ScBaseCvTankvolume selectByPrimaryId(Integer id) throws Exception{
        LOGGER.info("ScBaseCvTankvolumeServiceImpl.selectByPrimaryKey param : {}", id);

        if (null == id) {
            LOGGER.error("ScBaseCvTankvolumeServiceImpl.selectByPrimaryKey param id must not be null");
            throw new IllegalArgumentException("主键不能为空");
        }
        return this.scBaseCvTankvolumeMapper.selectByPrimaryKey(id);
    }


    @Override
    public void del(Integer id) throws Exception{
        LOGGER.info("ScBaseCvTankvolumeServiceImpl.del param : {}", id);

        if ( null == id || 0 == id.intValue()  ) {
            LOGGER.error("ScBaseCvTankvolumeServiceImpl.del param id must not be null");
            throw new IllegalArgumentException("主键不能为空");
        }
        scBaseCvTankvolumeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ScBaseCvTankvolumeVo selectCvTankvolumeByCvTypeId(Integer vehicleClassifyId) throws Exception{
        LOGGER.info("ScBaseCvTankvolumeServiceImpl.selectCvTankvolumeByCvTypeId param : {}", vehicleClassifyId);
        if ( null == vehicleClassifyId || 0 == vehicleClassifyId.intValue()  ) {
            LOGGER.error("ScBaseCvTankvolumeServiceImpl.selectCvTankvolumeByCvTypeId param id must not be null");
            throw new IllegalArgumentException("车系id不能为空");
        }

        ScBaseCvTankvolumeExample example = new ScBaseCvTankvolumeExample();
        example.createCriteria().andCvTypeIdEqualTo(vehicleClassifyId);

        List<ScBaseCvTankvolumeVo> list = scBaseCvTankvolumeMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }

        return null;
    }

    private void verificationForm(BindingResult br) throws Exception {

        if (null == br) {
            LOGGER.error("DVehicleClassifyServiceImpl.verificationForm error br is null");
            throw new IllegalArgumentException("BindingResult is null");
        }

        if (br.hasErrors()) {
            List<ObjectError> list = br.getAllErrors();
            LOGGER.error("DVehicleClassifyServiceImpl.add error : {}", list.get(0).getDefaultMessage());
            throw new IllegalArgumentException(list.get(0).getDefaultMessage());
        }
    }

}
